package hot100;

import java.util.ArrayList;
import java.util.List;

public class Lc199 {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> ret = new ArrayList<>();
        if (root == null) {
            return ret;
        }
        List<TreeNode> line = new ArrayList<>();
        List<TreeNode> temp = new ArrayList<>();
        line.add(root);
        while (!line.isEmpty()) {
            ret.add(line.getLast().val);

            for (TreeNode node : line) {
                if (node.left != null) {
                    temp.add(node.left);
                }
                if (node.right != null) {
                    temp.add(node.right);
                }
            }

            line.clear();
            line.addAll(temp);
            temp.clear();
        }
        return ret;
    }
}
